﻿using System;
using System.ComponentModel;
using System.Device.Location;
using System.IO.IsolatedStorage;
using System.Windows;
using System.Collections.ObjectModel;
using Microsoft.Phone.Controls.Maps;
using System.Collections.Generic;

namespace AuchanScan.ViewModels
{
    public class MaVoitureViewModel : INotifyPropertyChanged
    {
        public MaVoitureViewModel()
        {
            if (IsolatedStorageSettings.ApplicationSettings.Contains("MapLng") &&
                IsolatedStorageSettings.ApplicationSettings.Contains("MapLat"))
                CenterMap = new GeoCoordinate {
                    Longitude = (double)IsolatedStorageSettings.ApplicationSettings["MapLng"],
                    Latitude = (double)IsolatedStorageSettings.ApplicationSettings["MapLat"]
                };
            else
                CenterMap = new GeoCoordinate { Latitude = 48.858333, Longitude = 2.294444 };

            if (IsolatedStorageSettings.ApplicationSettings.Contains("VoitMapLng") &&
                IsolatedStorageSettings.ApplicationSettings.Contains("VoitMapLat"))
                VoitureMap = new GeoCoordinate
                {
                    Longitude = (double)IsolatedStorageSettings.ApplicationSettings["VoitMapLng"],
                    Latitude = (double)IsolatedStorageSettings.ApplicationSettings["VoitMapLat"]
                };
            else
                VoitureMap = new GeoCoordinate { Latitude = 48.858333, Longitude = 2.294444 };
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;
        public void RaisePropertyChanged(String property)
        {
            if (PropertyChanged == null)
                return;
            PropertyChanged(this, new PropertyChangedEventArgs(property));
        }

        #endregion

        public GeoCoordinate CenterMap { get; set; }

        public GeoCoordinate VoitureMap { get; set; }

        public void ComputeCoordinate(GeoCoordinate coord)
        {
            if (coord == null) {
                coord = CenterMap;
            }

            Deployment.Current.Dispatcher.BeginInvoke(() => {
                CenterMap = coord;
                RaisePropertyChanged("CenterMap");
            });
        }

        public String DistanceDisplay(double distance)
        {
            if (distance < 1)
                return ((int)(distance * 100)) + "m";
            else
                return distance.ToString("#.0") + "km";
        }

        public void Save()
        {
            IsolatedStorageSettings.ApplicationSettings["MapLng"] = CenterMap.Longitude;
            IsolatedStorageSettings.ApplicationSettings["MapLat"] = CenterMap.Latitude;

            IsolatedStorageSettings.ApplicationSettings["VoitMapLng"] = VoitureMap.Longitude;
            IsolatedStorageSettings.ApplicationSettings["VoitMapLat"] = VoitureMap.Latitude;
        }
    }
}
